using System.Text;
using Microsoft.AspNetCore.Mvc;
using Org.Apache.Rocketmq;
using RocketMQ.Api.Tests.Message;
using RocketMQ.Api.Tests.Producer;
using Vin.AspNetCore.Core.Controller;
using Vin.Extension.RocketMQ.Service;
using Vin.Tool.Attributes;

namespace RocketMQ.Api.Tests.Controllers;

public class TestController : VinBaseController
{
    [Inject] private readonly ProducerFactory _producerFactory = null!;

    [HttpGet("/normal")]
    public async Task<IActionResult> Test(string? data)
    {
        data ??= "Hello, RocketMQ!";
        var producer = await _producerFactory.GetProducer<TestNormalProducer1>();
        var taskList = new List<Task<ISendReceipt?>>();
        for (var i = 0; i < 10; i++)
        {
            var message = new TestNormalMessage1().CreateMessage(data + ":" + i);
            taskList.Add(producer.Send(message));
        }

        await Task.WhenAll(taskList);
        var res = taskList.Select(x => x.Result?.MessageId ?? "消息发送失败").ToList();

        return Ok(res);

        // var producer = await ProducerInstance.GetProducer(Constants.Topic);
        // var bytes = Encoding.UTF8.GetBytes(data);
        // var message = new Message.Builder()
        //     .SetTopic(Constants.Topic)
        //     .SetBody(bytes)
        //     .SetKeys(Guid.NewGuid().ToString())
        //     .Build();

        // return Ok((await producer.Send(message))?.MessageId ?? "消息发送失败");
        // return Ok();
    }
}